#!/bin/bash
# Copyright 2016 Martin Schröder <mkschreder.uk@gmail.com>

if [ "$1" = "" ]; then
	echo "Usage: extract-strings <file>"
	exit 1
fi 

for file in "$@"; do
	# extract all angular filter translations
	grep -RPoin "{{[[:space:]]*'.+?'[[:space:]]*\|[[:space:]]*translate[[:space:]]*}}" $file|awk "{ match(\$0, /'(.*)'/, arr); gsub(/\"/, \"'\", arr[1]); if(arr[1] != \"\") print \"# $file \nmsgid \\\"\" arr[1] \"\\\"\nmsgstr \\\"\\\"\n\" }"
	# extract all html tag translations
	grep -RPoin "<([a-z\-_0-9]+?).*?[[:space:]]translate.*?>(.+?)<\/\1>" $file | awk "{ match(\$0, /translate.*?>(.*?)<.*/, arr); gsub(/\"/, \"'\", arr[1]); if(arr[1] != \"\") print \"# $file \nmsgid \\\"\" arr[1] \"\\\"\nmsgstr \\\"\\\"\n\"; }"
	# extract all gettext(...) translations
	grep -RPoin "gettext\(\"(.+?)\"\)" $file | awk "{ match(\$0, /gettext\(\"(.+?)\"\)/, arr); gsub(/\"/, \"'\", arr[1]); if(arr[1] != \"\") print \"# $file \nmsgid \\\"\" arr[1] \"\\\"\nmsgstr \\\"\\\"\n\"; }"
done
